﻿using Commonality.DAL.MySQL;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Commonality.Model;

namespace Merge.BLL
{
    public static class PacketDataErasure
    {
        private static Timer timerErasure;
        public static int ErasureTime { get; set; }
        public static void PacketDataErasureInit()
        {
            ErasureTime = 10;

            timerErasure = new Timer();
            timerErasure.Tick += timerErasureFunction;
            timerErasure.Interval = 60000;
            timerErasure.Enabled = true;
            timerErasure.Start();
        }
        public static void PacketDataErasureCellAdd(string usnKey)
        {
            MySqlFunction.PacketDataErasure.PacketDataErasure_INSERT(usnKey);
        }
        public static Action<string> PacketDataErasureCellAddAction1 = (string usnKey) => { PacketDataErasureCellAdd(usnKey); };
        public static void PacketDataErasureCellAdd(string usnKey, int erasureTime)
        {
            MySqlFunction.PacketDataErasure.PacketDataErasure_INSERT(usnKey, erasureTime);
        }
        public static Action<string, int> PacketDataErasureCellAddAction2 = (string usnKey, int erasureTime) => { PacketDataErasureCellAdd(usnKey, erasureTime); };
        private static void timerErasureFunction(object sender, EventArgs e)
        {
            List<MySqlFunction.PacketDataErasure.PacketDataErasureStruct> packetDataErasureStructs = new List<MySqlFunction.PacketDataErasure.PacketDataErasureStruct>();
            MySqlFunction.PacketDataErasure.PacketDataErasure_SELECT(ref packetDataErasureStructs);
            foreach (MySqlFunction.PacketDataErasure.PacketDataErasureStruct item in packetDataErasureStructs)
            {
                if (item.Delayed < ErasureTime)
                {
                    int delayed = item.Delayed + 1;
                    MySqlFunction.PacketDataErasure.PacketDataErasureStruct_UPDATE(item.Usn, delayed);
                }
                else
                {
                    MySqlFunction.PacketData.DELETE_PacketData.Packet_Data_Delete("'" + item.Usn + "'");
                    MySqlFunction.PacketDataErasure.PacketDataErasureStruct_Delete(item.Usn);
                    Log4Net.MsgInfo(item.Usn + "包数据过时擦除");
                }
            }


        }
    }
}
